<table id="devices_mange_datagrid" title="" class="easyui-datagrid" style="width: 100%" url="/admin/devices/manage"
    toolbar="#devices-manage-datagrid-toolbar" pagination="true" pageSize="50" rownumbers="true" nowrap="false"
    checkOnSelect="false" singleSelect="false" tabPosition="bottom" fitColumns="true" fit="true" showFooter="true"
    striped="false" data-options="">
    <thead>
        <tr>
            <th data-options="field:'ck',checkbox:true"></th>
            <th data-options="field:'codeserial',align:'center',width:100">设备编号</th>
            <th data-options="field:'device_category',align:'center',width:100">设备类型</th>
            <th data-options="field:'device_type',align:'center',width:150">设备型号</th>
            <th data-options="field:'place_id',align:'center',width:150">所属场所</th>
            <th data-options="field:'device_address',align:'center',width:150">设备位置</th>
            <th data-options="field:'check_cycle',align:'center',width:150">巡检周期</th>
            <th data-options="field:'manager',align:'center',width:100">联系人</th>
            <th data-options="field:'device_id',align:'center',formatter:DevicesMangeModel.operate,width:100">管理员操作</th>
        </tr>
    </thead>
</table>
<style>
    label {
        margin-left: 8px;
    }
</style>
<div id="devices-manage-datagrid-toolbar" style="padding:5px;height:auto">
    <form style="margin:15px;">
        <label for="device_keyword">关键字：</label> <input type="text" class="easyui-textbox" id="device_keyword" style="margin-right: 5px">
        <select id="device_key_type" class="easyui-combobox" style="width: 150px;" panelMaxHeight="200px;" data-options="panelHeight:'auto'">
            <option value="0">全部条件</option>
            <option value="1">设备编号</option>
            <option value="2">设备地址</option>
        </select>
        <label for="device_place_id">所属场所：</label>
        <select name="" id="device_place_id" class="easyui-combobox" style="width: 150px;" panelMaxHeight="200px;"
            data-options="panelHeight:'auto'">
            <option value="0">全部</option>
            <volist name="place_config" id="name">
                <option value="<{$key}>">
                    < {$name}>
                </option>
            </volist>
        </select>
        <a href="javascript:void(0);" onclick="DevicesMangeModel.search(this);" class="easyui-linkbutton" data-options="plain:false,iconCls:'icons-table-table'">查询</a>

    </form>
    <a style="margin-left: 20px;" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:false,iconCls:'icons-table-table_add'"
        onclick="DevicesMangeModel.set_devices_info()">新增</a>
    <a style="margin-left: 10px;" href="javascript:void(0);" class="easyui-linkbutton mange-btn__delete" data-options="plain:false,iconCls:'icons-table-table_delete'"
        onclick="DevicesMangeModel.batch_delete()">删除</a>
    <a href="javascript:void(0);" class="easyui-linkbutton" style="margin-left: 20px;" data-options="plain:true,iconCls:'icons-table-table_refresh'"
        onclick="DevicesMangeModel.refresh()">刷新</a>
    <!--<a style="margin-left: 10px;" href="javascript:void(0);" class="easyui-linkbutton"-->
    <!--data-options="plain:false,iconCls:'icons-table-table_go'" onclick="DevicesMangeModel.export()">导入</a>-->
    <!--<a style="margin-left: 10px;" href="javascript:void(0);" class="easyui-linkbutton"-->
    <!--data-options="plain:false,iconCls:'icons-table-table_edit'" onclick="DevicesMangeModel.import()">导出</a>-->
</div>

<script type="text/javascript">

    var DevicesMangeModel = {
        dialog: '#globel-dialog-div',
        datagrid: '#devices_mange_datagrid',

        //操作格式化
        operate: function (val, arr) {
            var btn = [];
            btn.push('<a href="javascript:void(0);" onclick="DevicesMangeModel.set_devices_info(' + val + ')">编辑</a>');
            btn.push('<a href="javascript:void(0);" onclick="DevicesMangeModel.delete(' + val + ')">删除</a>');
            return btn.join(' |  ');
        },

        //刷新
        refresh: function () {
            $(this.datagrid).datagrid('reload');
        },

        //数据查询
        search: function () {
            var keyword = $("#device_keyword").val();
            var key_type = $('#device_key_type').combobox('getValue');
            var place_id = $('#device_place_id').combobox('getValue');
            $(this.datagrid).datagrid("load", {
                keyword: keyword,
                key_type: key_type,
                place_id: place_id
            });
        },

        //新增
        set_devices_info: function (id) {
            var that = this;
            var href = "<{:U('admin/devices/set_devices_info')}>";
            if (id > 0) {
                href += href.indexOf('?') != -1 ? '&id=' + id : '?id=' + id;
            }
            $(that.dialog).dialog({
                title: '新增/编辑',
                iconCls: 'icons-application-application_add',
                width: 800,
                height: 600,
                cache: false,
                href: href,
                modal: true,
                collapsible: false,
                minimizable: false,
                resizable: false,
                maximizable: false,
                buttons: [{
                    text: '确定',
                    iconCls: 'icons-other-tick',
                    handler: function () {
                        $(that.dialog).find('form').eq(0).form('submit', {
                            onSubmit: function () {
                                var isValid = $(this).form('validate');
                                if (!isValid) {
                                    return false;
                                }
                                $.messager.progress({ text: '处理中，请稍候...' });
                                $.post("<{:U('admin/devices/set_devices_info')}>", $(this).serialize(), function (res) {
                                    $.messager.progress('close');
                                    if (res.code != '0000') {
                                        $.app.method.tip('提示信息', res.msg, 'error');
                                    }
                                    else {
                                        $.app.method.tip('提示信息', res.msg, 'info');
                                        $(that.dialog).dialog('close');
                                        that.refresh();
                                    }
                                }, 'json'
                                );
                                return false;
                            }
                        });
                    }
                }, {
                    text: '取消',
                    iconCls: 'icons-arrow-cross',
                    handler: function () {
                        $(that.dialog).dialog('close');
                    }
                }]
            });
        },


        //删除
        delete: function (id) {
            if (typeof (id) !== 'number') {
                $.app.method.tip('提示信息', '未选择删信息', 'error');
                return false;
            }
            var that = this;
            $.messager.confirm('提示信息', '确定要删除吗？', function (result) {
                if (!result) return false;

                $.messager.progress({ text: '处理中，请稍候...' });
                $.post("<{:U('admin/devices/delete_devices')}>", { id: id }, function (res) {
                    $.messager.progress('close');
                    if (!res.status) {
                        $.app.method.tip('提示信息', res.info, 'error');
                    } else {
                        $.app.method.tip('提示信息', res.info, 'info');
                        that.refresh();
                    }
                }, 'json');
            });
        },
        batch_delete: function () {
            var checkedItems = $(this.datagrid).datagrid('getChecked');
            var ids = [];
            if (checkedItems.length <= 0) return;
            $.each(checkedItems, function (index, item) {
                ids.push(item.device_id);
            });
            var str = ids.join(",");
            var that = this;
            $.messager.confirm('提示信息', '确定要删除吗？', function (result) {
                if (!result) return false;
                $.messager.progress({ text: '处理中，请稍候...' });
                $.post("<{:U('admin/devices/delete_devices')}>", { id: str }, function (res) {
                    $.messager.progress('close');
                    if (!res.status) {
                        $.app.method.tip('提示信息', res.info, 'error');
                    } else {
                        $.app.method.tip('提示信息', res.info, 'info');
                        that.refresh();
                    }
                }, 'json');
            });
        },

        //导出
        export: function () {
            $.messager.progress({ text: '处理中，请稍候...' });
            $.post('<{:U("admin/System/menuExport")}>', function (res) {
                $.messager.progress('close');

                if (!res.status) {
                    $.app.method.tip('提示信息', res.info, 'error');
                } else {
                    $.app.method.tip('提示信息', res.info, 'info');
                    window.location.href = res.url;
                }
            }, 'json');
        },

        //导入
        import: function () {
            var that = this;
            $.messager.confirm('提示信息', '该操作将清空所有数据，确定要继续吗？', function (result) {
                if (!result) return false;

                $.app.method.upload(
                    '<{:U("admin/Upload/import", array('from'=>urlencode('设备管理')))}>',
                    function (json) {    //上传成功回调函数

                        $.messager.progress({ text: '处理中，请稍候...' });
                        $.post('<{:U('admin / System / menuImport')}>', { filename: json.filename }, function (res) {
                            $.messager.progress('close');

                            if (!res.status) {
                                $.app.method.tip('提示信息', res.info, 'error');
                            } else {
                                $.app.method.tip('提示信息', res.info, 'info');
                                that.refresh();
                            }
                        }, 'json');
                    },
                    function (filename) {  //上传验证函数
                        if (!filename.match(/\.data$/)) {
                            $.app.method.tip('提示信息', '上传文件后缀不允许', 'error');
                            return false;
                        }
                        return true;
                    }
                );
            });
        },
        checkSelectorLength: function () {
            var btn = '.mange-btn__delete';
            var checkedItems = $(datagrid).datagrid('getChecked');
            var btnEle = $(btn);
            if (checkedItems.length) {
                btnEle.removeClass('is-disabled');
            } else {
                btnEle.hasClass('is-disabled') || btnEle.addClass('is-disabled');
            }
        }
    };
    var datagrid = '#devices_mange_datagrid';
    $(datagrid).datagrid({
        onLoadSuccess: function (data) {
            DevicesMangeModel.checkSelectorLength();
        },
        onCheck: function (rowIndex, rowData) {
            DevicesMangeModel.checkSelectorLength();
        },
        onUncheck: function (rowIndex, rowData) {
            DevicesMangeModel.checkSelectorLength();
        },
        onCheckAll: function (rowIndex, rowData) {
            DevicesMangeModel.checkSelectorLength();
        },
        onUncheckAll: function (rowIndex, rowData) {
            DevicesMangeModel.checkSelectorLength();
        },
    })

</script>